<HTML><HEAD><TITLE>Manpage of tkdnd</TITLE>
</HEAD><BODY>
<H1>tkdnd</H1>
Section: Tk Built-In Commands (n)<BR>Updated: 8.4<BR><A HREF="#index">Index</A>
<A HREF="http://localhost/cgi-bin/man/man2html">Return to Main Contents</A><HR>


<BR>



<PRE>
<BR>
<HR></PRE>




<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>

tkdnd - Tk Drag and Drop Interface
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>

<B>tkdnd::drop_target register</B><I> window ?type-list?</I>
<BR>

<B>tkdnd::drop_target unregister</B><I> window</I>
<P>

<B>tkdnd::drag_source register</B><I> window ?type-list? ?mouse-button?</I>
<BR>

<B>tkdnd::drag_source unregister</B><I> window</I>
<P>

<B>tkdnd::platform_specific_types</B><I> type-list</I>
<BR>

<B>tkdnd::platform_independent_types</B><I> type-list</I>
<P>

<B>tkdnd::GetDropFileTempDirectory</B>
<BR>

<B>tkdnd::SetDropFileTempDirectory </B><I>directory</I>
<P>
<A NAME="lbAD">&nbsp;</A>
<H2>INTRODUCTION</H2>

The <B>tkdnd</B> family commands provide a Tcl interface to native,
platform specific drag and drop mechanism.
Under unix the drag &amp; drop protocol in use is the XDND protocol version 5
(also used by the Qt toolkit, and the KDE and GNOME desktops).
Under Windows, the OLE2 drag &amp; drop interfaces are used.
Under Macintosh, the Cocoa drag &amp; drop interfaces are used.
<P>

The <B>tkdnd</B> family can be built from source code using the standard configure; make; make install procedure. Cmake is also supported. Some binary package are available from the SourceForge download site, from ActiveState's ActiveTcl distribution, and elsewhere.
<P>

With the <B>tkdnd</B> family commands the user is able to register existing Tk
widgets as drag sources or drop targets, that are able to send or receive data
during drag and drop operations, encoded in specific types.
These types can be platform independent types like DND_Text and DND_Files or
platform specific types, like CF_UNICODETEXT or text/plain.
<P>

The legal forms for the <B>tkdnd</B> commands are:
<DL COMPACT>
<DT><B>tkdnd::drop_target register</B><I> window ?type-list?</I><DD>
This command will register <I>window</I> as a drop target. A drop target is a
widget than can accept a drop action. <I>window</I> must exist when this command
is executed and this command can be executed multiple times on a widget.
<P>
When <I>window</I> is registered as a drop target, and optional <I>type-list</I>
can be provided. This type list can contain one or more types that <I>window</I>
will accept during a drop action, and it can contain platform independent or
platform specific types. Platform independent are <B>DND_Text</B>
for dropping text portions and <B>DND_Files</B> for dropping a list of files
(which can contain one or multiple files) on <I>window</I>.
If <I>type-list</I> is not specified, it defaults to the empty list.
<DT><B>tkdnd::drop_target unregister</B><I> window</I><DD>
This command will stop <I>window</I> from being a drop target.
Thus, <I>window</I> will stop receiving events related to drop operations.
It is an error to use this command for a <I>window</I> that has not been
registered as a drop target with <B>tkdnd::drop_target register</B>.
<DT><B>tkdnd::drag_source register</B><I> window ?type-list? ?mouse-button?</I><DD>
This command will register <I>window</I> as a drag source. A drag source is a
widget than can start a drag action. <I>window</I> must exist when this command
is executed and this command can be executed multiple times on a widget.
<P>
When <I>window</I> is registered as a drag source, and optional <I>type-list</I>
can be provided. This type list can contain one or more types that <I>window</I>
will provide during a drag action, and it can contain platform independent or
platform specific types. Platform independent are <B>DND_Text</B>
for dropping text portions and <B>DND_Files</B> for dropping a list of files
(which can contain one or multiple files) on <I>window</I>. However, this type
list is indicative/informative. <I>window</I> can initiate a drag action with
even a different type list. If <I>type-list</I> is not specified, it defaults to
the empty list.
<P>
Finally, <I>mouse-button</I> is the mouse button that will be used for starting
the drag action. It can have any of the values 1 (left mouse button),
2 (middle mouse button - wheel) and 3 (right mouse button).
If <I>mouse-button</I> is not specified, it defaults to 1.
<DT><B>tkdnd::drag_source unregister</B><I> window</I><DD>
This command will stop <I>window</I> from being a drag source.
Thus, <I>window</I> will stop receiving events related to drag operations.
It is an error to use this command for a <I>window</I> that has not been
registered as a drag source with <B>tkdnd::drag_source register</B>.
<DT><B>tkdnd::platform_specific_types </B><I>type-list</I><DD>
This command will accept a list of types that can contain platform independnent
or platform specific types. A new list will be returned, where each platform
independent type in <I>type-list</I> will be substituted by one or more platform
specific types. Thus, the returned list may have more elements than
<I>type-list</I>.
<DT><B>tkdnd::platform_independent_types</B><I> type-list</I><DD>
This command will accept a list of types that can contain platform independnent
or platform specific types. A new list will be returned, where each platform
specific type in <I>type-list</I> will be substituted by one or more platform
independent types. Thus, the returned list may have more elements than
<I>type-list</I>.
<DT><B>tkdnd::GetDropFileTempDirectory</B><DD>
This command will return the temporary directory used by TkDND for storing
temporary files. When the package is loaded, this temporary directory will be
initialised to a proper directory according to the operating system. This
default initial value can be changed to be the value of the following
environmental variables: <B>TKDND_TEMP_DIR</B>, <B>TEMP</B>, <B>TMP</B>.
<DT><B>tkdnd::SetDropFileTempDirectory </B><I>directory</I><DD>
This command will change the temporary directory used by TkDND for storing
temporary files to <I>directory</I>.
</DL>
<A NAME="lbAE">&nbsp;</A>
<H2>SUPPORTED TYPES</H2>

In order to declare the format that the data that will transfered during
a drag and drop operation, all drag and drop protocols use the notion of types.
Unfortunately, each protocol defines its own, usually platform specific, types. Tkdnd, trying to maintain portability among different platforms, offers some
predefined types for some basic kinds of data, like text, and filenames.
Currently, the following predifined cross-platform values are available:
<DL COMPACT>
<DT><B>DND_Text</B>:<DD>
This type can be used for transfering textual data. Internally, it is translated
to the following platform specific formats:
<BR>

Windows: <B>CF_UNICODETEXT</B>, <B>CF_TEXT</B>.
<BR>

Unix: <B>text/plain;charset=UTF-8</B>, <B>text/plain</B>.
<BR>

Mac: <B>NSStringPboardType</B>
  <BR>
<DT><B>DND_Files</B>:<DD>
This type can be used for transfering a list of file names.
Internally, it is translated to the following platform specific formats:
<BR>

Windows: <B>CF_HDROP</B>.
<BR>

Unix: <B>text/uri-list</B>.
  <BR>

Mac: <B>NSFilenamesPboardType</B>.
<DT>Additionally to the platform independent types, tkdnd supports the following platform specific types:<DD>
<DT><B>Windows</B>:<DD>
<B>CF_UNICODETEXT</B>: Text transfer encoded in unicode.
<BR>

<B>CF_TEXT</B>: Text transfer with application dependent encoding. If an encoding
locale is specified through CF_LOCALE it is used, else the system encoding is
used for the conversion.
<BR>

<B>FileGroupDescriptor - FileContents</B>: These two types are used for
transfering a set of files that do not appear physically on disk, like files
from compressed folders or Outlook e-mail messages. File names are transfered as
in the CF_TEXT type, while file contents are transfered in binary. Tkdnd
retrieves both the file names and the file contents, and saves then in a
temporary directory. When the transfer is complete, the file names of the saved
files in the temporary folder are returned. Note that tkdnd support this type
pair only as drop targets and not as drag sources.
<BR>

<B>FileGroupDescriptorW - FileContents</B>: These two types are used for
transfering a set of files that do not appear physically on disk, like files
from compressed folders or Outlook e-mail messages. File names are transfered as
in the CF_UNICODETEXT type, while file contents are transfered in binary. Tkdnd
retrieves both the file names and the file contents, and saves then in a
temporary directory. When the transfer is complete, the file names of the saved
files in the temporary folder are returned. Note that tkdnd support this type
pair only as drop targets and not as drag sources.
<BR>

<B>CF_HDROP</B>: Files transfer encoded in UTF-8.
<DT><B>Unix</B>:<DD>
</DL>
<P>

Finally, format types used for drop types can have <B>wildcards</B>, following
the same rules as &quot;string match&quot;. For example, registering a drop target with
the type &quot;*&quot;, will accept any drop, no matter what the drop format is.
<A NAME="lbAF">&nbsp;</A>
<H2>EVENTS</H2>

Windows registered as either drop targets or drag sources, will receive certain
events, during drag and drop operations. As a result, the windows are expected
to have bindings for some of these events. Some events are mandatory (in the
sense that a drag or drop operation can be stopped if the bindings do not
exist), while others are not. However, it is a good practice to define bindings
for all events, so as the application will behave as expected during drag and
drop operations.
<P>
The tkdnd package defines a set of virtual events, that correspond to various
phases of a drag and drop operation. All windows that are either a drop target
or a drag source are expected to have bindings for (all) these events, created
with the <B>bind</B> Tk command. While these event bindings are regular Tk
events, they have a small difference from plain Tk events: most of them are
expected to return a value. Since Tk bindings cannot return a value, tkdnd does
not actually generate these events (i.e. through &quot;event generate&quot;). Instead,
tkdnd locates the script that has been bound to the virtual event, and directly
executes it, as a script. Apart from this small difference in how the events are
triggered, the events required by tkdnd can be viewed as regular events. In the
following two sections all virtual events defined by the tkdnd package are
presented.
<A NAME="lbAG">&nbsp;</A>
<H2>DROP TARGET EVENTS</H2>

A window registered as a drop target, is expected to have bindings for the
following virtual events:
<DL COMPACT>
<DT><B>&lt;&lt;DropEnter&gt;&gt;</B>:<DD>
This event is triggered when the mouse enters the window
during a drop action. The purpose of this event is to change the visual state of
the window, so as to notify the user whether the drop will be accepted or not.
The binding script is expected to return a single value that will define the
drop action. This returned action can be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
<DT><B>&lt;&lt;DropPosition&gt;&gt;</B>:<DD>
This events is triggered when the mouse moves inside the
window during a drop action. The purpose of this event is to let window decide
if it will accept the drop and the action of the drop, if a drop is going to
happen at the specific mouse coordinates. Thus, the script binding for such an
event can get the mouse coordinates and the pressed modifier buttons (such as
ctrl, shift or alt), and is expected to return the drop action, which again must
be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
<DT><B>&lt;&lt;DropLeave&gt;&gt;</B>:<DD>
This event is triggered when the mouse leaves outside the
area covered by window, whithout a drop happening. The binding of such an event
is expected to restore the visual state of the window to normal (i.e. the visual
state the window was in before the &lt;&lt;DropEnter&gt;&gt; event was triggered). The
binding for such an event is not expected to return a value.
<DT><B>&lt;&lt;Drop&gt;&gt;</B>:<DD>
This event is triggered by a drop action, and it is expected to
handle the dropped data and reset the visual state of the window. The binding
script is expected to return a value, which will be the action that has been
performed to the data and must be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
<DT><B>&lt;&lt;Drop:</B><I>type</I><B>&gt;&gt;</B>:<DD>
This event is a specialisation of the generic
&lt;&lt;Drop&gt;&gt; event, augmented with a type. If such a binding exists and the drop
type matches <I>type</I>, this event binding will be executed, instead of the
generic &lt;&lt;Drop&gt;&gt; event binding. These events allow for easy specialisation of
drop bindings, according to the type of the dropped data. <I>type</I> can be
either a platform independent or a platform specific type. The binding script of
such an event is expected to return a value, which will be the action that has
been performed to the data and must be one of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, <B>private</B> and <B>refuse_drop</B>. This event is not
mandatory, but if it is defined, it has to return an action. In case an action
is not returned, the drop is refused for this window.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>DRAG SOURCE EVENTS</H2>

A window registered as a drag source, is expected to have bindings for the
following virtual events:
<DL COMPACT>
<DT><B>&lt;&lt;DragInitCmd&gt;&gt;</B>:<DD>
This event is triggered when a drag action is about to start. This is a
mandatory event (whose absence will cancel the drag action), and is responsible
for providing three things: the list of actions and format types supported by
the drag source, and of course the data to be dropped. Thus, the binding script
for such an event must return a list of three elements: the drop actions
supported by the drag source (which can be any of <B>copy</B>, <B>move</B>,
<B>link</B>, <B>ask</B>, and <B>private</B>), the format type list that the data
can be dropped as (which can be any platform independent or platform specific
type), and finally the data. A simple example of such a binding, is:

<DL COMPACT><DT><DD>
<PRE>

bind .drag_source &lt;&lt;DragInitCmd&gt;&gt; \
     {list copy DND_Text {Hellow world!}}

</PRE>

</DL>


<DT><B>&lt;&lt;DragEndCmd&gt;&gt;</B>:<DD>
This event is triggered when the drag action has finished (either when the drop
was succesful or not). Its main purpose is to process the dropped data according
to the drop action returned by the drop target. Binding for such an event
is not mandatory, and the binding is not expected to return a value.
</DL>
<A NAME="lbAI">&nbsp;</A>
<H2>BINDING SCRIPTS AND SUBSTITUTIONS</H2>

All bindings scripts defined for any of the virtual events above will be
executed in the same interpreter that was used for creating the window the 
event is bound to, and the binding script will executed at the global
level (i.e. only global variables will be accessible).
<P>
If the binding script contains any % characters, then substitutions will be
made, like in normal Tk binding scripts. Valid % specifiers are:
<DL COMPACT>
<DT><B>%%</B><DD>
Replaced with a single percent. 
<DT><B>%A</B><DD>
The current action of the drag/drop operation.
<DT><B>%a</B><DD>
The action list supported by the drag source. 
<DT><B>%b</B><DD>
The mouse button that is pressed during a drag/drop operation. Note that 
always a single mouse button is reported as pressed, even if more than one 
mouse buttons are actually pressed.
<DT><B>%c</B><DD>
The codes of the list of types supported by the drag source. All codes are in
octal format and have the same order as the list of types obtained through the
<B>%t</B> substitution.
<DT><B>%C</B><DD>
The code (in octal format) of the current type of the drag and drop operation.
<DT><B>%CTT</B><DD>
The list of types from the drop target type list that are common to the drag
source type list.
<DT><B>%CST</B><DD>
The list of types from the drag source type list that are common to the drop
target type list.
<DT><B>%D</B><DD>
The data that has been dropped. Under some platforms the data will be 
available before the drop has occured. The format of the data is the current
type of the drop operation.
<DT><B>%e</B><DD>
The name of the current virtual event. One of &lt;&lt;DropEnter&gt;&gt;, &lt;&lt;DropPosition&gt;&gt;,
&lt;&lt;DropLeave&gt;&gt;, &lt;&lt;Drop:<I>type</I>&gt;&gt;, &lt;&lt;Drop&gt;&gt;, &lt;&lt;DragInitCmd&gt;&gt;, &lt;&lt;DragEndCmd&gt;&gt;.
<DT><B>%L</B><DD>
The list of types supported by the drag source.
<DT><B>%m</B><DD>
The list of modifier keyboard keys that are pressed. 
Modifier keys are some special keys, like Shift, Control or Alt. 
Valid modifiers are &quot;shift&quot;, &quot;ctrl&quot; and &quot;alt&quot;. It is useful for
binding scripts of drop target events to examine this list of modifiers, as it
is quite usuall to change the action according to the state of some modifier
keys.
<DT><B>%ST</B><DD>
The list of types supported by the drag source.
<DT><B>%t</B><DD>
The list of types supported by the drag source.
<DT><B>%T</B><DD>
The current type of the drag and drop operation.
<DT><B>%TT</B><DD>
The list of types supported by the drop target.
<DT><B>%W</B><DD>
The window that the event is delivered to.
<DT><B>%X</B><DD>
The mouse pointer x coordinate, relative to the root window.
<DT><B>%Y</B><DD>
The mouse pointer y coordinate, relative to the root window.
</DL>
<A NAME="lbAJ">&nbsp;</A>
<H2>SPECIFYING DROP TARGETS</H2>

Creating drop targets is easy: we have to only register a window as a drop
target with the list of format types it can accept, and add a few bindings. For
example, a window that accepts textual drops can be as follows:

<DL COMPACT><DT><DD>
<PRE>

label .drop_target -text {Text Drop Target!} -bg white
tkdnd::drop_target register .drop_target DND_Text
bind .drop_target &lt;&lt;DropEnter&gt;&gt; {%W configure -bg yellow; list copy}
bind .drop_target &lt;&lt;DropPosition&gt;&gt; {list copy}
bind .drop_target &lt;&lt;DropLeave&gt;&gt; {%W configure -bg white}
bind .drop_target &lt;&lt;Drop&gt;&gt; {%W configure -text %D; %W configure -bg white}

</PRE>

</DL>


From the above bindings, none is obligatory. However, we usually want to receive
dropped data (thus the &lt;&lt;Drop&gt;&gt; event must be handled) and we want to give
visual feedback to the users through the &lt;&lt;DropEnter&gt;&gt; and &lt;&lt;DropLeave&gt;&gt; events.
Finally, &lt;&lt;DropPosition&gt;&gt; is only necessary if we want to only accept drops on
specific areas of the window, or we want to change the drop action according to
the pressed modifiers.
<P>
Now, if we want to also add the ability to receive file drops, we could add:

<DL COMPACT><DT><DD>
<PRE>

tkdnd::drop_target register .drop_target DND_Files
bind .drop_target &lt;&lt;Drop:DND_Files&gt;&gt; \
   {puts %D; %W configure -bg white}

</PRE>

</DL>


Note that we have added a &quot;specialised&quot; drop binding, for the event
&lt;&lt;Drop:DND_Files&gt;&gt;: this means that when a text portion is dropped over the
window, the &lt;&lt;Drop&gt;&gt; event binding will be executed. But when a list of files is
dropped onto the window, the &lt;&lt;Drop:DND_Files&gt;&gt; event binding will be executed.
If we proceed and define a binding for the &lt;&lt;Drop:DND_Text&gt;&gt; event, the binding
of the &quot;general&quot; &lt;&lt;Drop&gt;&gt; event will never be executed.
<A NAME="lbAK">&nbsp;</A>
<H2>SPECIFYING DRAG SOURCES</H2>

In order to specify a drag source, we need to register a window as a drag
source:

<DL COMPACT><DT><DD>
<PRE>

tkdnd::drag_source register .text_drag_source

</PRE>

</DL>


The above command defines a drag source with an empty type list (and which will
be declared in the &lt;&lt;DragInitCmd&gt;&gt; event binding) and arranges mouse bindings
such as a drag will be started with the left mouse button.
Then, it is absolutely necessary to define a binding for the &lt;&lt;DragInitCmd&gt;&gt;:
this event binding must return the list of actions, the list of format types and
the actual data to be dropped:

<DL COMPACT><DT><DD>
<PRE>

bind .text_drag_source &lt;&lt;DragInitCmd&gt;&gt; \
     {list {copy move} DND_Text {Hello from Tk!}}

</PRE>

</DL>


And that was all!
<A NAME="lbAL">&nbsp;</A>
<H2>BUGS</H2>

<B>Unix:</B>
Dragging from Tk windows has not yet been implemented.
<BR>
<B>OS X:</B>
TkDND under OS X is built on top of the Cocoa framework. Thus, it is compatible
with Tk version &gt;= 8.6
<BR>

<A NAME="lbAM">&nbsp;</A>
<H2>KEYWORDS</H2>

dnd, drag and drop
<P>

<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT><A HREF="#lbAB">NAME</A><DD>
<DT><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT><A HREF="#lbAD">INTRODUCTION</A><DD>
<DT><A HREF="#lbAE">SUPPORTED TYPES</A><DD>
<DT><A HREF="#lbAF">EVENTS</A><DD>
<DT><A HREF="#lbAG">DROP TARGET EVENTS</A><DD>
<DT><A HREF="#lbAH">DRAG SOURCE EVENTS</A><DD>
<DT><A HREF="#lbAI">BINDING SCRIPTS AND SUBSTITUTIONS</A><DD>
<DT><A HREF="#lbAJ">SPECIFYING DROP TARGETS</A><DD>
<DT><A HREF="#lbAK">SPECIFYING DRAG SOURCES</A><DD>
<DT><A HREF="#lbAL">BUGS</A><DD>
<DT><A HREF="#lbAM">KEYWORDS</A><DD>
</DL>
<HR>
This document was created by
<A HREF="http://localhost/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 23:51:03 GMT, January 03, 2010
</BODY>
</HTML>
